﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class SkinsListVCData
{
    public List<SkinVCData> skins;
    public int defaultSkin;
}

public class SkinVCData
{
    public int skinId;
    public bool unlock;
    public int price;
}



public class SkinsListViewController : ViewController, ISkinsListPanel
{
    private MBattleSceneController battleSceneController => GetComponentInParent<MBattleSceneController>();
    MBattleController battleScene =>battleSceneController .mBattleController;

    SkinsListPanel m_Panel;
    public SkinsListPanel panel => m_Panel;

    SkinsListVCData m_Data;

    int m_CurSkinId;

    public SkinsListPanel Init(SkinsListVCData data)
    {
        if (m_Panel == null)
            m_Panel = InitUI();

        GameDebug.LogError($"data != null:{data != null}");

        m_Data = data;
        m_CurSkinId = data.defaultSkin;
        m_Panel.Init(data);

        return m_Panel;
    }

    protected override void WillLoadView()
    {
        base.WillLoadView();
        
        this.battleScene.ShowSkinCam();
    }

    protected virtual SkinsListPanel InitUI()
    {
        SkinsListPanel prefab = ResourceManager.Load<SkinsListPanel>(ViewPath.skinsListPanel);
        if (prefab == null)
        {
            GameDebug.LogError("skinsListPanel prefab null");
            return null;
        }
        m_Panel = GameObject.Instantiate<SkinsListPanel>(prefab, transform);
        m_Panel.uiListener = this;
        m_Panel.SetViewController(this);
        // panel.name = "战斗UI";
        return m_Panel;
    }

    public void OnSelectSkin(int skinId)
    {
        if (battleScene.skinId != skinId)
        {
            GameDebug.LogError($"选择皮肤：{skinId}");

            ChangePlayerSkin(skinId, true);
        }
    }
    public void OnConfirm()
    {
        if (MGame.Instance)
        {
            //if (!MGame.instance.MUser.IsSkinUnlock(battleScene.skinId))
            //{
            //    GameDebug.Log($"皮肤{battleScene.skinId} lock, return to ：{m_CurSkinId}");
            //    ChangePlayerSkin(m_CurSkinId, false);
            //}
            //else
            //{
            //    //mBattleController.me.skinId = m_CurSkinId;
            //    m_CurSkinId = battleScene.skinId;
            //    GameDebug.LogError($"确认皮肤：{m_CurSkinId}");

            //    MGame.instance.MUser.UpdateCurSkinId(m_CurSkinId);

            //    ChangePlayerSkin(m_CurSkinId, false);
            //}
        }
    }

    public override void WillHideView()
    {
        base.WillHideView();

        if (m_Panel)
        {
            m_Panel.PlayHideAnim();
        }

        OnConfirm();

        this.battleScene.ShowNormalCam();
    }

    void ChangePlayerSkin(int skinId,bool temp)
    {
        battleScene.OnUpdateRole(skinId);
    }

    public override void OnViewDelete()
    {
        base.OnViewDelete();

        if (m_Panel)
        {
            Destroy(m_Panel.gameObject);
        }
        m_Panel = null;
    }

    public void OnBuy()
    {
        var skin = ConstantData.instance.GetSkin(battleScene.skinId);
        if (skin != null)
        {
            int price = skin.price;

            if(MGame.instance.user.coin >= price)
            {
                bool suc = MGame.instance.MUser.AddSkin(battleScene.skinId);
                if (suc)
                {
                    var data = this.m_Data.skins.Find(e => e.skinId == battleScene.skinId);
                    data.unlock = true;
                    m_Panel.UpdateSkin(data);

                    GameDebug.Log($"buy skin suc");
#if GameAnalytics
                    GameAnalyticsSDK.GameAnalytics.NewDesignEvent($"Buy_Skin_{m_CurSkinId}");
#endif
                }
            }
            else
            {
                MGame.instance.ShowNoticePanel(Localization.instance.GetValue("coin_not_enough"));
            }

        }
    }
    public static SkinsListVCData GetSkinsListVCData(MGameUser gameUser)
    {
        SkinsListVCData skinsListVCData = new SkinsListVCData();
        skinsListVCData.skins = ConstantData.instance.skins.Map(e =>
        {
            SkinVCData skinData = new SkinVCData();

            skinData.skinId = e.skinId;
            skinData.price = e.price;

            //if (e.skinId == ConstantData.defaultSkin || gameUser.IsSkinUnlock(e.skinId))
            //{
            //    skinData.unlock = true;
            //}
            //else
            //{
            //    skinData.unlock = false;
            //}
            return skinData;
        }).AsList();

        int skinId = gameUser.myApplyNum;
        if (skinId <= 0)
            skinId = ConstantData.defaultSkin;

        skinsListVCData.defaultSkin = skinId;

        return skinsListVCData;
    }
}